import * as constants from './constants';
import axios from 'axios';
import Cookies from "js-cookie";
import {fromJS} from 'immutable';

const changeCategory = (data) => ({
    type: constants.GET_CATEGORY,
    data: fromJS(data),
});


export const getCategory = () => {
    return (dispatch) => {
        axios.get('/type/list').then((res) => {
            if (res.success) {
                dispatch(changeCategory(res.data));
            }
        })
    }
};

export const getUser = () => {
    return (dispatch) => {
        const user = Cookies.get('user')
        if (!user) {
            axios.get('/user/info').then((res) => {
                if (res.success) {
                    dispatch({
                        type: constants.GET_USER,
                        data: fromJS(res.data)
                    });
                }
            })
        }
    }
};

export const getConfing = () => {
    return (dispatch) => {
        axios.get('/config/get').then((res) => {
            if (res.success) {
                dispatch({
                    type: constants.GET_CONFING,
                    data: fromJS(res.data)
                });
            }
        })
    }
};
